﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Xml.Serialization;
using WebSite.App_Data;

namespace WebSite.App_Code
{
    ///// <summary>
    ///// 项目预算信息
    ///// </summary>
    //public class ProjectEntity
    //{
    //    public ProjectEntity() 
    //    {
    //        BaseInfo = new md_ProjectInfo();
    //        Budget = new md_ProjectBudget();
    //        PreviousExpense = new md_ProjectExpense();
    //        BudgetExpense = new md_ProjectExpense();
    //        ActualExpense = new md_ProjectExpense();
    //        AdvanceInfo = new md_ProjectAdvance();
    //        ProjectCategory = new list_ProjectCategory();
    //        ProjectStatus = new list_ProjectStatus();
    //        IsAdvance = new list_YesOrNo();
    //        PreviousExpense.ExpenseType = (int)ProjectExpenseTypeEnum.PreviousExpense;
    //        ActualExpense.ExpenseType = (int)ProjectExpenseTypeEnum.ActualExpense;
    //        BudgetExpense.ExpenseType = (int)ProjectExpenseTypeEnum.BudgetExpense;
    //    }

    //    /// <summary>
    //    /// 支出类型
    //    /// </summary>
    //    enum ProjectExpenseTypeEnum
    //    {
    //        /// <summary>
    //        /// 历年支出
    //        /// </summary>
    //        PreviousExpense = 0,
    //        /// <summary>
    //        /// 当年支出（实际）
    //        /// </summary>
    //        ActualExpense = 1,
    //        /// <summary>
    //        /// 当年支出（预算）
    //        /// </summary>
    //        BudgetExpense = 2
    //    }

    //    /// <summary>
    //    /// 项目状态
    //    /// </summary>
    //    enum ProjectStatusEnum
    //    {
    //        /// <summary>
    //        /// 开放提交
    //        /// </summary>
    //        Open = 0,
    //        /// <summary>
    //        /// 审核中
    //        /// </summary>
    //        Audit = 20,
    //        /// <summary>
    //        /// 启用
    //        /// </summary>
    //        InUse = 40,
    //        /// <summary>
    //        /// 调整中
    //        /// </summary>
    //        Adjust = 60,
    //        /// <summary>
    //        /// 完成
    //        /// </summary>
    //        Complete = 80,
    //        /// <summary>
    //        /// 关闭
    //        /// </summary>
    //        Close = 100
    //    }

    //    /// <summary>
    //    /// 项目类别
    //    /// </summary>
    //    enum ProjectCategoryEnum
    //    {
    //        /// <summary>
    //        /// 预算科研
    //        /// </summary>
    //        BudgetResearch = 1,
    //        /// <summary>
    //        /// 预算外科研
    //        /// </summary>
    //        BudgetSurgeryResearch = 2,
    //        /// <summary>
    //        /// 有偿服务
    //        /// </summary>
    //        PaidServices = 3,
    //    }

    //    /// <summary>
    //    /// 项目类别
    //    /// </summary>
    //    public list_ProjectCategory ProjectCategory { get; set; }
        
    //    /// <summary>
    //    /// 项目状态
    //    /// </summary>
    //    public list_ProjectStatus ProjectStatus { get; set; }

    //    /// <summary>
    //    /// 是否垫支
    //    /// </summary>
    //    public list_YesOrNo IsAdvance { get; set; }

    //    /// <summary>
    //    /// 项目信息
    //    /// </summary>
    //    public md_ProjectInfo BaseInfo { get; set; }

    //    /// <summary>
    //    /// 预算金额
    //    /// </summary>
    //    public md_ProjectBudget Budget { get; set; }

    //    /// <summary>
    //    /// 历年支出
    //    /// </summary>
    //    public md_ProjectExpense PreviousExpense { get; set; }

    //    /// <summary>
    //    /// 当年支出（实际）
    //    /// </summary>
    //    public md_ProjectExpense ActualExpense { get; set; }

    //    /// <summary>
    //    /// 当年支出（预算）
    //    /// </summary>
    //    public md_ProjectExpense BudgetExpense { get; set; }

    //    /// <summary>
    //    /// 垫支信息
    //    /// </summary>
    //    public md_ProjectAdvance AdvanceInfo { get; set; }

    //    /// <summary>
    //    /// 预算金额[总计]
    //    /// </summary>
    //    public decimal? BudgetAmount { get { return Budget.Balance + Budget.OtherIncome + Budget.ProjectFunds + Budget.StandardFunds + Budget.Subvention; } }

    //    /// <summary>
    //    /// 历年支出[总计]
    //    /// </summary>
    //    public decimal? PreviousExpenseAmount { get { return PreviousExpense.BusinessFee + PreviousExpense.CooperationFee + PreviousExpense.EquipmentCost + PreviousExpense.MaterialCost + PreviousExpense.PendingFunds; } }

    //    /// <summary>
    //    /// 当年支出（实际）[总计]
    //    /// </summary>
    //    public decimal? ActualExpenseAmount { get { return ActualExpense.BusinessFee + ActualExpense.CooperationFee + ActualExpense.EquipmentCost + ActualExpense.MaterialCost + ActualExpense.PendingFunds; } }

    //    /// <summary>
    //    /// 当年支出（预算）[总计]
    //    /// </summary>
    //    public decimal? BudgetExpenseAmount { get { return BudgetExpense.BusinessFee + BudgetExpense.CooperationFee + BudgetExpense.EquipmentCost + BudgetExpense.MaterialCost + BudgetExpense.PendingFunds; } }

    //    /// <summary>
    //    /// 插入项目预算信息
    //    /// </summary>
    //    /// <param name="ProList"></param>
    //    public static void InsertProject(List<ProjectEntity> ProList)
    //    {
    //        DBDataContext DB = new DBDataContext();

    //        DB.Connection.Open();
    //        DB.Transaction = DB.Connection.BeginTransaction();

    //        try
    //        {
    //            IEnumerable<list_ProjectCategory> ieProC = DB.list_ProjectCategories;
    //            IEnumerable<list_ProjectStatus> ieProS = DB.list_ProjectStatus;
    //            IEnumerable<list_YesOrNo> ieYN = DB.list_YesOrNos;

    //            foreach (ProjectEntity pro in ProList)
    //            {
    //                //对于项目类型赋予int
    //                if (ieProC.Select(o => o.Name == pro.ProjectCategory.Name).Any())
    //                    pro.BaseInfo.CatrgoryID = (from x in ieProC
    //                                               where x.Name == pro.ProjectCategory.Name
    //                                               select x.ID).First();

    //                //对于项目状态赋予int
    //                if (ieProS.Select(o => o.Name == pro.ProjectStatus.Name).Any())
    //                    pro.BaseInfo.Status = (from x in ieProS
    //                                           where x.Name == pro.ProjectStatus.Name
    //                                           select x.Status).First();

    //                //赋予项目是否有垫付bit
    //                if (ieYN.Select(o => o.Name == pro.IsAdvance.Name).Any())
    //                    pro.BaseInfo.IsAdvance = (from x in ieYN
    //                                              where x.Name == pro.IsAdvance.Name
    //                                              select x.Value).First();

    //                //判断该年度-项目代码是否存在
    //                IEnumerable<md_ProjectInfo> ieProInfo =
    //                    from pi in DB.md_ProjectInfos
    //                    where pi.Year == pro.BaseInfo.Year
    //                    where pi.ProjectCode == pro.BaseInfo.ProjectCode
    //                    where pi.CatrgoryID == pro.BaseInfo.CatrgoryID
    //                    select pi;

    //                Guid ProjectID;
    //                if (ieProInfo.Any())
    //                {
    //                    ProjectID = ieProInfo.First().Guid;

    //                    //有该数据，删除已有数据
    //                    DB.md_ProjectInfos.DeleteOnSubmit(ieProInfo.First());
    //                    DB.md_ProjectAdvances.DeleteAllOnSubmit(
    //                        from pa in DB.md_ProjectAdvances
    //                        where pa.ProjGuid == ieProInfo.First().Guid
    //                        select pa);
    //                    DB.md_ProjectBudgets.DeleteAllOnSubmit(
    //                        from pb in DB.md_ProjectBudgets
    //                        where pb.ProjGuid == ieProInfo.First().Guid
    //                        select pb);
    //                    DB.md_ProjectExpenses.DeleteAllOnSubmit(
    //                        from pe in DB.md_ProjectExpenses
    //                        where pe.ProjGuid == ieProInfo.First().Guid
    //                        select pe);                        
    //                    DB.SubmitChanges();
    //                }
    //                else
    //                {
    //                    //执行新建
    //                    ProjectID = Guid.NewGuid();
    //                }

    //                pro.BaseInfo.Guid = ProjectID;
    //                pro.AdvanceInfo.ProjGuid = ProjectID;
    //                pro.Budget.ProjGuid = ProjectID;
    //                pro.PreviousExpense.ProjGuid = ProjectID;
    //                pro.ActualExpense.ProjGuid = ProjectID;
    //                pro.BudgetExpense.ProjGuid = ProjectID;

    //                pro.Budget.Guid = Guid.NewGuid();
    //                pro.AdvanceInfo.Guid = Guid.NewGuid();
    //                pro.PreviousExpense.Guid = Guid.NewGuid();
    //                pro.ActualExpense.Guid = Guid.NewGuid();
    //                pro.BudgetExpense.Guid = Guid.NewGuid();

    //                DB.md_ProjectInfos.InsertOnSubmit(pro.BaseInfo);
    //                DB.md_ProjectAdvances.InsertOnSubmit(pro.AdvanceInfo);
    //                DB.md_ProjectBudgets.InsertOnSubmit(pro.Budget);
    //                DB.md_ProjectExpenses.InsertOnSubmit(pro.PreviousExpense);
    //                DB.md_ProjectExpenses.InsertOnSubmit(pro.ActualExpense);
    //                DB.md_ProjectExpenses.InsertOnSubmit(pro.BudgetExpense);
    //            }

    //            DB.SubmitChanges();
    //            DB.Transaction.Commit();
    //        }
    //        catch (Exception ex)
    //        {
    //            DB.Transaction.Rollback();
    //            throw ex;
    //        }
    //        finally
    //        {
    //            DB.Connection.Close();
    //            DB.Dispose();
    //            DB = null;
    //        }
    //    }

    //    /// <summary>
    //    /// 查询项目预算信息
    //    /// </summary>
    //    /// <returns></returns>
    //    public static List<vw_Project> SelectProject() {

    //        List<vw_Project> lsReturn = new List<vw_Project>();

    //        DBDataContext DB = new DBDataContext();

    //        DB.Connection.Open();

    //        try
    //        {
    //            if (DB.vw_Projects.Any())
    //                lsReturn = DB.vw_Projects.ToList();
    //        }
    //        catch (Exception)
    //        {
    //        }
    //        finally
    //        {
    //            DB.Connection.Close();
    //            DB.Dispose();
    //            DB = null;
    //        }

    //        return lsReturn;
    //    }

    //    public static List<vw_Project> SelectProject(IEnumerable<Guid> ieID)
    //    {
    //        List<vw_Project> lsReturn = new List<vw_Project>();

    //        DBDataContext DB = new DBDataContext();

    //        DB.Connection.Open();

    //        try
    //        {
    //            IEnumerable<vw_Project> ieReturn = from p in DB.vw_Projects
    //                                               where ieID.Contains(p.ProjectID)
    //                                               select p;

    //            if (ieReturn.Any())
    //                lsReturn = ieReturn.ToList();
    //        }
    //        catch (Exception)
    //        {
    //        }
    //        finally
    //        {
    //            DB.Connection.Close();
    //            DB.Dispose();
    //            DB = null;
    //        }

    //        return lsReturn;
    //    }
    //}
}
